package com.xingongchang.baolebei.fragment;

import java.util.HashMap;

import org.json.JSONException;
import org.json.JSONObject;

import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.external.androidquery.callback.AjaxStatus;
import com.insthub.BeeFramework.model.BusinessResponse;
import com.xingongchang.baolebei.R;
import com.xingongchang.baolebei.model.ProtocolConst;
import com.xingongchang.baolebei.model.ShoppingCartModel;
import com.xingongchang.baolebei.model.UserInfoModel;

public class TabsFragment extends Fragment
{

	ImageView tab_one;
	ImageView tab_two;
	ImageView tab_three;
	ImageView tab_four;
	ImageView toolbar_tabfive;

	LinearLayout toolbar_home;
	LinearLayout toolbar_show;
	LinearLayout toolbar_cart;
	LinearLayout toolbar_person;
	LinearLayout toolbar_tao;

	TextView toolbar_textone;
	TextView toolbar_texttwo;
	TextView toolbar_textthree;
	TextView toolbar_textfour;
	TextView toolbar_textfive;

	private static TextView shopping_cart_num;
	private static LinearLayout shopping_cart_num_bg;

	private SharedPreferences shared;
	private SharedPreferences.Editor editor;
	HomeFragment homeFragment;
	CategoryFragment forumFragment;
	ShoppingCartFragment shoppingCartFragment;
	ProfileFragment profileFragment;
	ForumFragment fourmFragment;
	Forumfrag forumfrag;
	private static Fragment.SavedState fgState = null;
	UserInfoModel userInfoModel;
	NetworkInfo mNetworkInfo;// 判断网络是否连接

	public TabsFragment()
	{
	}

	/*
	 * (non-Javadoc)
	 * 
	 * 
	 * android.view.ViewGroup, android.os.Bundle)
	 */
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState)
	{
		View mainView = inflater.inflate(R.layout.toolbar, container, false);
		init(mainView);
		shared = getActivity().getSharedPreferences("userInfo", 0);
		editor = shared.edit();
		// 判断是否连接网络
		ConnectivityManager mConnectivityManager = (ConnectivityManager) getActivity()
				.getSystemService(Context.CONNECTIVITY_SERVICE);
		mNetworkInfo = mConnectivityManager.getActiveNetworkInfo();
		return mainView;
	}

	@TargetApi(Build.VERSION_CODES.HONEYCOMB)
	public void onActivityCreated(Bundle paramBundle)
	{
		super.onActivityCreated(paramBundle);
		setRetainInstance(true);
		// userInfoModel = new UserInfoModel(getActivity());
		// userInfoModel.addResponseListener(this);
	}

	void init(View mainView)
	{

		this.shopping_cart_num = (TextView) mainView
				.findViewById(R.id.shopping_cart_num);
		this.shopping_cart_num_bg = (LinearLayout) mainView
				.findViewById(R.id.shopping_cart_num_bg);

		this.tab_one = (ImageView) mainView.findViewById(R.id.toolbar_tabone);
		this.toolbar_home = (LinearLayout) mainView
				.findViewById(R.id.toolbar_home);
		this.toolbar_textone = (TextView) mainView
				.findViewById(R.id.toolbar_textone);
		this.toolbar_home.setOnClickListener(new View.OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				OnTabSelected("tab_one");
			}
		});

		this.tab_two = (ImageView) mainView.findViewById(R.id.toolbar_tabtwo);
		this.toolbar_show = (LinearLayout) mainView
				.findViewById(R.id.toolbar_show);
		this.toolbar_texttwo = (TextView) mainView
				.findViewById(R.id.toolbar_texttwo);
		this.toolbar_show.setOnClickListener(new View.OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				OnTabSelected("tab_two");
			}
		});
		this.tab_three = (ImageView) mainView
				.findViewById(R.id.toolbar_tabthree);
		this.toolbar_person = (LinearLayout) mainView
				.findViewById(R.id.toolbar_person);
		this.toolbar_textthree = (TextView) mainView
				.findViewById(R.id.toolbar_textthree);
		this.toolbar_person.setOnClickListener(new View.OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				OnTabSelected("tab_three");
			}
		});

		this.tab_four = (ImageView) mainView.findViewById(R.id.toolbar_tabfour);
		this.toolbar_cart = (LinearLayout) mainView
				.findViewById(R.id.toolbar_cart);
		this.toolbar_textfour = (TextView) mainView
				.findViewById(R.id.toolbar_textfour);
		this.toolbar_cart.setOnClickListener(new View.OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				OnTabSelected("tab_four");
			}
		});

		this.toolbar_tabfive = (ImageView) mainView
				.findViewById(R.id.toolbar_tabfive);
		this.toolbar_tao = (LinearLayout) mainView
				.findViewById(R.id.toolbar_tao);
		this.toolbar_textfive = (TextView) mainView
				.findViewById(R.id.toolbar_textfive);
		this.toolbar_tao.setOnClickListener(new View.OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				OnTabSelected("tab_five");
			}
		});

		OnTabSelected("tab_one");
	}

	HashMap<String, Fragment> fragments = new HashMap<String, Fragment>();

	void OnTabSelected(String tabName)
	{
		homeFragment = (HomeFragment) fragments.get("tab_one");
		forumFragment = (CategoryFragment) fragments.get("tab_two");
		forumfrag = (Forumfrag) fragments.get("tab_three");
		shoppingCartFragment = (ShoppingCartFragment) fragments.get("tab_four");
		profileFragment = (ProfileFragment) fragments.get("tab_five");

		if (tabName == "tab_one")
		{
			if (null == homeFragment)
			{
				homeFragment = new HomeFragment();
				fragments.put(tabName, homeFragment);
			}

			FragmentTransaction localFragmentTransaction = getFragmentManager()
					.beginTransaction();

			if (homeFragment.isAdded())
			{
				homeFragment.onResume();
			} else
			{
				localFragmentTransaction.add(R.id.fragment_container,
						homeFragment);
			}
			localFragmentTransaction.show(homeFragment);

			if (forumFragment != null)
			{
				localFragmentTransaction.hide(forumFragment);
			}
			if (forumfrag != null)
			{
				localFragmentTransaction.hide(forumfrag);
			}
			if (shoppingCartFragment != null)
			{
				localFragmentTransaction.hide(shoppingCartFragment);
			}
			if (profileFragment != null)
			{
				localFragmentTransaction.hide(profileFragment);
			}
			localFragmentTransaction.commit();

			this.tab_one.setImageResource(R.drawable.menu_home_sel);
			this.tab_two.setImageResource(R.drawable.menu_classify);
			this.tab_three.setImageResource(R.drawable.menu_communication);
			this.tab_four.setImageResource(R.drawable.menu_cart);
			this.toolbar_tabfive.setImageResource(R.drawable.menu_profiles);

			this.toolbar_textone.setTextColor(this.getResources().getColor(
					R.color.title_select));
			this.toolbar_texttwo.setTextColor(this.getResources().getColor(
					R.color.title_custom));
			this.toolbar_textthree.setTextColor(this.getResources().getColor(
					R.color.title_custom));
			this.toolbar_textfour.setTextColor(this.getResources().getColor(
					R.color.title_custom));
			this.toolbar_textfive.setTextColor(this.getResources().getColor(
					R.color.title_custom));

		}
		if (tabName == "tab_two")
		{

			if (null == forumFragment)
			{
				forumFragment = new CategoryFragment();
				fragments.put(tabName, forumFragment);
			}
			FragmentTransaction localFragmentTransaction = getFragmentManager()
					.beginTransaction();
			if (forumFragment.isAdded())
			{
				forumFragment.onResume();
			} else
			{
				localFragmentTransaction.add(R.id.fragment_container,
						forumFragment);
			}
			if (homeFragment != null)
			{
				localFragmentTransaction.hide(homeFragment);
			}
			if (forumfrag != null)
			{
				localFragmentTransaction.hide(forumfrag);
			}
			if (shoppingCartFragment != null)
			{
				localFragmentTransaction.hide(shoppingCartFragment);
			}
			if (profileFragment != null)
			{
				localFragmentTransaction.hide(profileFragment);
			}
			localFragmentTransaction.show(forumFragment);

			localFragmentTransaction.commit();

			this.tab_one.setImageResource(R.drawable.menu_home);
			this.tab_two.setImageResource(R.drawable.menu_classify_sel);
			this.tab_three.setImageResource(R.drawable.menu_communication);
			this.tab_four.setImageResource(R.drawable.menu_cart);
			this.toolbar_tabfive.setImageResource(R.drawable.menu_profiles);

			this.toolbar_textone.setTextColor(this.getResources().getColor(
					R.color.title_custom));
			this.toolbar_texttwo.setTextColor(this.getResources().getColor(
					R.color.title_select));
			this.toolbar_textthree.setTextColor(this.getResources().getColor(
					R.color.title_custom));
			this.toolbar_textfour.setTextColor(this.getResources().getColor(
					R.color.title_custom));
			this.toolbar_textfive.setTextColor(this.getResources().getColor(
					R.color.title_custom));

		}
		if (tabName == "tab_three")
		{
			if (null == forumfrag)
			{
				forumfrag = new Forumfrag();
				fragments.put(tabName, forumfrag);
			}
			FragmentTransaction localFragmentTransaction = getFragmentManager()
					.beginTransaction();
			if (forumfrag.isAdded())
			{
				forumfrag.onResume();
			} else
			{
				localFragmentTransaction
						.add(R.id.fragment_container, forumfrag);
			}
			if (homeFragment != null)
			{
				localFragmentTransaction.hide(homeFragment);
			}
			if (forumFragment != null)
			{
				localFragmentTransaction.hide(forumFragment);
			}
			if (shoppingCartFragment != null)
			{
				localFragmentTransaction.hide(shoppingCartFragment);
			}
			if (profileFragment != null)
			{
				localFragmentTransaction.hide(profileFragment);
			}
			localFragmentTransaction.show(forumfrag);

			localFragmentTransaction.commit();

			this.tab_one.setImageResource(R.drawable.menu_home);
			this.tab_two.setImageResource(R.drawable.menu_classify);
			this.tab_three.setImageResource(R.drawable.menu_communication_sel);
			this.tab_four.setImageResource(R.drawable.menu_cart);
			this.toolbar_tabfive.setImageResource(R.drawable.menu_profiles);

			this.toolbar_textone.setTextColor(this.getResources().getColor(
					R.color.title_custom));
			this.toolbar_texttwo.setTextColor(this.getResources().getColor(
					R.color.title_custom));
			this.toolbar_textthree.setTextColor(this.getResources().getColor(
					R.color.title_select));
			this.toolbar_textfour.setTextColor(this.getResources().getColor(
					R.color.title_custom));
			this.toolbar_textfive.setTextColor(this.getResources().getColor(
					R.color.title_custom));
		}
		if (tabName == "tab_four")
		{

			if (null == shoppingCartFragment)
			{
				shoppingCartFragment = new ShoppingCartFragment();
				fragments.put(tabName, shoppingCartFragment);
			}
			FragmentTransaction localFragmentTransaction = getFragmentManager()
					.beginTransaction();
			if (shoppingCartFragment.isAdded())
			{
				shoppingCartFragment.onResume();
			} else
			{
				localFragmentTransaction.add(R.id.fragment_container,
						shoppingCartFragment);
			}
			if (homeFragment != null)
			{
				localFragmentTransaction.hide(homeFragment);
			}
			if (forumFragment != null)
			{
				localFragmentTransaction.hide(forumFragment);
			}
			if (forumfrag != null)
			{
				localFragmentTransaction.hide(forumfrag);
			}
			if (profileFragment != null)
			{
				localFragmentTransaction.hide(profileFragment);
			}
			localFragmentTransaction.show(shoppingCartFragment);

			localFragmentTransaction.commit();

			this.tab_one.setImageResource(R.drawable.menu_home);
			this.tab_two.setImageResource(R.drawable.menu_classify);
			this.tab_three.setImageResource(R.drawable.menu_communication);
			this.tab_four.setImageResource(R.drawable.menu_cart_sel);
			this.toolbar_tabfive.setImageResource(R.drawable.menu_profiles);

			this.toolbar_textone.setTextColor(this.getResources().getColor(
					R.color.title_custom));
			this.toolbar_texttwo.setTextColor(this.getResources().getColor(
					R.color.title_custom));
			this.toolbar_textthree.setTextColor(this.getResources().getColor(
					R.color.title_custom));
			this.toolbar_textfour.setTextColor(this.getResources().getColor(
					R.color.title_select));
			this.toolbar_textfive.setTextColor(this.getResources().getColor(
					R.color.title_custom));

		}

		if (tabName == "tab_five")
		{
			if (null == profileFragment)
			{
				profileFragment = new ProfileFragment();
				fragments.put(tabName, profileFragment);
			}
			FragmentTransaction localFragmentTransaction = getFragmentManager()
					.beginTransaction();
			if (profileFragment.isAdded())
			{
				profileFragment.onResume();
			} else
			{
				localFragmentTransaction.add(R.id.fragment_container,
						profileFragment);
			}
			if (homeFragment != null)
			{
				localFragmentTransaction.hide(homeFragment);
			}
			if (forumFragment != null)
			{
				localFragmentTransaction.hide(forumFragment);
			}
			if (forumfrag != null)
			{
				localFragmentTransaction.hide(forumfrag);
			}
			if (shoppingCartFragment != null)
			{
				localFragmentTransaction.hide(shoppingCartFragment);
			}
			localFragmentTransaction.show(profileFragment);

			localFragmentTransaction.commit();

			this.tab_one.setImageResource(R.drawable.menu_home);
			this.tab_two.setImageResource(R.drawable.menu_classify);
			this.tab_three.setImageResource(R.drawable.menu_communication);
			this.tab_four.setImageResource(R.drawable.menu_cart);
			this.toolbar_tabfive.setImageResource(R.drawable.menu_profiles_sel);

			this.toolbar_textone.setTextColor(this.getResources().getColor(
					R.color.title_custom));
			this.toolbar_texttwo.setTextColor(this.getResources().getColor(
					R.color.title_custom));
			this.toolbar_textthree.setTextColor(this.getResources().getColor(
					R.color.title_custom));
			this.toolbar_textfour.setTextColor(this.getResources().getColor(
					R.color.title_custom));
			this.toolbar_textfive.setTextColor(this.getResources().getColor(
					R.color.title_select));
		}
	}



	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent data)
	{

		super.onActivityResult(requestCode, resultCode, data);

		// 登录成功返回到个人主页
		if (resultCode == 1)
		{
			if (data != null)
			{
				profileFragment = new ProfileFragment();
				FragmentTransaction localFragmentTransaction = getFragmentManager()
						.beginTransaction();
				localFragmentTransaction.replace(R.id.fragment_container,
						profileFragment, "tab_five");
				localFragmentTransaction.commit();

				this.tab_one.setImageResource(R.drawable.home);
				this.tab_two.setImageResource(R.drawable.search);
				this.tab_three.setImageResource(R.drawable.menu_communication);
				this.tab_four.setImageResource(R.drawable.menu_cart);
				this.toolbar_tabfive
						.setImageResource(R.drawable.menu_profiles_sel);

				this.toolbar_textone.setTextColor(this.getResources().getColor(
						R.color.title_custom));
				this.toolbar_texttwo.setTextColor(this.getResources().getColor(
						R.color.title_custom));
				this.toolbar_textthree.setTextColor(this.getResources()
						.getColor(R.color.title_custom));
				this.toolbar_textfour.setTextColor(this.getResources()
						.getColor(R.color.title_custom));
				this.toolbar_textfive.setTextColor(this.getResources()
						.getColor(R.color.title_select));
			}
		} else if (requestCode == 2)
		{
			if (null != data)
			{
				if (null == shoppingCartFragment)
				{
					shoppingCartFragment = new ShoppingCartFragment();
				}
				FragmentTransaction localFragmentTransaction = getFragmentManager()
						.beginTransaction();
				localFragmentTransaction.replace(R.id.fragment_container,
						shoppingCartFragment, "tab_four");
				localFragmentTransaction.commit();

				this.tab_one.setImageResource(R.drawable.home);
				this.tab_two.setImageResource(R.drawable.search);
				this.tab_three.setImageResource(R.drawable.menu_communication);
				this.tab_four.setImageResource(R.drawable.menu_cart_sel);
				this.toolbar_tabfive.setImageResource(R.drawable.menu_profiles);
				this.toolbar_textone.setTextColor(this.getResources().getColor(
						R.color.title_custom));
				this.toolbar_texttwo.setTextColor(this.getResources().getColor(
						R.color.title_custom));
				this.toolbar_textthree.setTextColor(this.getResources()
						.getColor(R.color.title_custom));
				this.toolbar_textfour.setTextColor(this.getResources()
						.getColor(R.color.title_select));
				this.toolbar_textfive.setTextColor(this.getResources()
						.getColor(R.color.title_custom));
			}

		}
	}

	@Override
	public void onResume()
	{
		super.onResume();
		if(mNetworkInfo!=null){
			setShoppingcartNum();
		}
	}

	public static void setShoppingcartNum()
	{
		// shopping_cart_num.setText("12");
		if (ShoppingCartModel.getInstance().goods_num == 0)
		{
			shopping_cart_num_bg.setVisibility(View.GONE);
		} else
		{
			shopping_cart_num_bg.setVisibility(View.VISIBLE);
			shopping_cart_num.setText(ShoppingCartModel.getInstance().goods_num
					+ "");
		}
	}

	// @Override
	// public void OnMessageResponse(String url, JSONObject jo, AjaxStatus
	// status)
	// throws JSONException {
	// if (url.endsWith(ProtocolConst.USERINFO)) {
	// if(userInfoModel.responseStatus.succeed==1){
	// userInfoModel.getUserInfo();
	// }
	// }
	// }

}